package com.xuzy.x808.server.processor;

import cn.hutool.core.date.DateUtil;
import com.xuzy.x808.common.utils.ByteUtils;
import com.xuzy.x808.entity.Location;
import com.xuzy.x808.entity.MsgHeader;
import com.xuzy.x808.entity.PackageData;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 终端位置上报 定时10s一次，或者事件触发立即上报
 */
@Component
public class LocationReportProcessor extends AbstractProcessor {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void process(ChannelHandlerContext ctx, PackageData packageData) {
        MsgHeader msgHeader = packageData.getMsgHeader();

        //解析位置上报协议
        ByteBuf msgBody = packageData.getMsgBody();
        int alarmFlag = msgBody.readInt();
        int status = msgBody.readInt();
        BigDecimal latitude = BigDecimal.valueOf(msgBody.readUnsignedInt() / 1000000.0).setScale(6, RoundingMode.HALF_UP);
        BigDecimal longitude = BigDecimal.valueOf(msgBody.readUnsignedInt() / 1000000.0).setScale(6, RoundingMode.HALF_UP);
        Integer height = (int) msgBody.readShort();
        BigDecimal speed = BigDecimal.valueOf(msgBody.readShort() / 10.0).setScale(1, RoundingMode.HALF_UP);
        Integer direction = (int) msgBody.readShort();
        String time = "20" + ByteUtils.bcdToString(ByteBufUtil.getBytes(msgBody, 22, 6));
        long timestamp = DateUtil.parse(time, "yyyyMMddHHmmss").getTime() / 1000;
        Integer ts = Integer.valueOf(Long.toString(timestamp));

        //如果msgBody存在附加消息，则消息体会大于28
        if (msgBody.readableBytes() > 28) {
            Integer msg0x01 = (int) msgBody.getUnsignedByte(28);
            Integer msg0x01Length = (int) msgBody.getUnsignedByte(29);
            long milages = msgBody.getUnsignedInt(30) / 10;

            Integer msg0x02 = (int) msgBody.getUnsignedByte(34);
            Integer msg0x02Length = (int) msgBody.getUnsignedByte(35);
            int fulCost = msgBody.getShort(36) / 10;

            //车辆信号状态
            Integer msg0x25 = (int) msgBody.getUnsignedByte(38);
            Integer msg0x25Length = (int) msgBody.getUnsignedByte(39);
            long signalStatus = msgBody.getUnsignedInt(40);

            //需人工确认报警事件
            Integer msg0x04 = (int) msgBody.getUnsignedByte(42);
            Integer msg0x04Length = (int) msgBody.getUnsignedByte(43);
            int humanConfirmAlarm = msgBody.getUnsignedShort(44);

            System.out.println(humanConfirmAlarm);
        }
        //todo:
        /**
         *
         * 也可以扩展这块协议，实现更多的功能，比如扩展前向、侧向雷达数据，刹车数据、刹车速度曲线，ttc时间等内容
         * 还可以将一些特定的事件，比如急减速、刹车等通过扩展的部分传上来，并指定一个字节定义为customerMsgId比如0x01
         * 代码实现上，只需要将customerMsgId维护在msgTypeEnum中，再在此处调用processServiceRegister.getProcessor(msgId).process(ctx, packageData);
         */

        Location location = new Location();
        location.setAlarmFlag(alarmFlag);
        location.setStatus(status);
        location.setLatitude(latitude);
        location.setLongitude(longitude);
        location.setHeight(height);
        location.setSpeed(speed);
        location.setDirection(direction);
        location.setTs(ts);

        //todo 使用location进行业务处理

        //response(ctx, msgHeader.getDevId(), msgHeader.getMsgId(), msgHeader.getSeq());
    }
}
